<?php defined('SYSPATH') OR die('No direct access allowed.');

class Forum_Question_Controller extends Template_Controller {

	const ALLOW_PRODUCTION = FALSE;
	public $template = 'layouts/template_forum';

	public function index(){
		url::redirect("forum/forum_question/listResultForumQuestion");
	}

	public function forumQuestionForm(){
		$this->template->content = new View('forum/forum_question_create');
		$this->template->content->forum_categories=ORM::factory("forum_category")->find_all();
		$this->template->content->captcha = Captcha::factory();
		$this->template->title = '';

	}

	public function insertForumQuestion(){

		if ($_POST){
			if (Captcha::valid($this->input->post('captcha_response'))){
				$forumQuestion = new Forum_Question_Model();
				$forumQuestion->headline=$this->input->post("headline");
				$forumQuestion->detail=$this->input->post("detail");
				$forumQuestion->forum_category_id=$this->input->post("forum_category_id");
				$forumQuestion->post_date=Forum_Question_Controller::getPostDate();
				$forumQuestion->user_id=1;
				$forumQuestion->count_view=1;
				$forumQuestion->save();
				url::redirect("forum/forum_question/listResultForumQuestion");
			}else{
				$this->template->content = new View('forum/forum_question_create');
				$this->template->content->forum_categories=ORM::factory("forum_category")->find_all();
				$this->template->content->captcha = Captcha::factory();
			}
		}else{
			$this->template->content = new View('forum/forum_question_create');
			$this->template->content->forum_categories=ORM::factory("forum_category")->find_all();
			$this->template->content->captcha = Captcha::factory();
		}
		$this->template->title = '';

	}

	public function listResultForumQuestion(){
		$total_items = ORM::factory("forum_question")
	    					->find_all()
	    					->count();
	    $pagination = new Pagination(array(
	        'base_url'    => 'forum/forum_question/listResultForumQuestion/',
	        'uri_segment' => 'listResultForumQuestion', 
	        'total_items' => $total_items,
	    	'items_per_page' => 20
	    ));
	    $this->template->content = new View('forum/forum_question_listResult_user');
	    $this->template->content->forum_questions =ORM::factory("forum_question")
	    											->limit($pagination->items_per_page,$pagination->sql_offset)
	    											->find_all();
	    											
	    $this->template->content->pagination=$pagination;					
		$this->template->title = '';

	}

	public function listResultForumQuestionByCatId(){
		if($this->input->get("id")!=null && $this->input->get("id")!=''){
			$this->template->content = new View('forum/forum_question_listResult');
			$this->template->content->forum_questions=ORM::factory("forum_question")
			->where('forum_category_id', $this->input->get("id"))
			->find_all();
			$this->template->title = '';
		}
	}
	public function findForumQuestionById(){
		if($this->input->get("id")!=null && $this->input->get("id")!=''){
			$total_items = ORM::factory("forum_answer")
	    					->where('forum_question_id', $this->input->get("id"))
							->find_all()
	    					->count();
		    $pagination = new Pagination(array(
		        'base_url'    => 'forum/forum_question/findForumQuestionById/',
		        'uri_segment' => 'findForumQuestionById', 
		        'total_items' => $total_items
		    ));
		    $this->template->content = new View('forum/forum_question_by_id');
		    $this->template->content->forum_questions =ORM::factory("forum_question")
		    											->where('id', $this->input->get("id"))
														->find_all();
		    $this->template->content->forum_answers =ORM::factory("forum_answer")
								    					->where('forum_question_id', $this->input->get("id"))
	    												->limit($pagination->items_per_page,$pagination->sql_offset)
	    												->find_all();
	    	$this->template->content->pagination=$pagination;	
	    	$this->template->content->captcha = Captcha::factory();
			$this->template->title = '';
		}
	}
	
	public function page($page_no){
		
		$total_items = ORM::factory("forum_question")
	    					->find_all()
	    					->count();
	     $pagination = new Pagination(array(
	        'base_url'    => 'forum/forum_question/page/',
	        'uri_segment' => 'page', 
	        'total_items' => $total_items
	    ));
	    
	    $this->template->content = new View('forum/forum_question_listResult');
	    $this->template->content->forum_questions =ORM::factory("forum_question")
	    											->limit($pagination->items_per_page,$pagination->sql_offset)
	    											->find_all(); 
	    											
	    $this->template->content->pagination=$pagination;	
	    $this->template->title = '';
	 
	}
	
	function getPostDate(){
		$db = new Database;
		$sql = 'select sysdate() as post_date from dual';
		$query = $db->query($sql);
		$strPostDate="";
		foreach($query as $item){
			$strPostDate = $item->post_date;
			break;
		}
		return $strPostDate;
	}
	public function getGetId(){
		echo "ID : ".$this->input->get("id");
	}

}
?>